﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.Generic;
using HRMSystem.DBServices;


namespace HRMSystem.Wpf.EmployeePages
{
    /// <summary>
    /// Interaction logic for Employee DetailsPage.xaml
    /// </summary>
    public partial class DetailsPage : Page
    {
        private static DetailsPage instance;
        public static IList<string> ErrorList { get; set; }

        private DetailsPage()
        {
            InitializeComponent();

            ErrorList = new List<string>();

            EmployeeContentFrame.Content = EmployeeInfo.Instance;
            ContactsContentFrame.Content = ContactsInfo.Instance;
            EmployementContentFrame.Content = EmploymentInfo.Instance;
        }

        public static DetailsPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DetailsPage();
                }

                return instance;
            }
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var h = ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualHeight;
            ScrollBar.Height = h-150;
        }

        public int SaveNewEmployeeDetails()
        {
            Employee employee = EmployeeInfo.Instance.GetEmployeeDetails();
            Title title = EmploymentInfo.Instance.GetEmploymentDetails();

            if (employee != null && title != null && ErrorList.Count == 0)
            {

                employee.USERNAME = title.com_employees.USERNAME;
                employee.PASSWORD = title.com_employees.PASSWORD;

                List<Contact> contacts = ContactsInfo.Instance.GetContactDetails();

                int result = 0;

                result = EmployeeService.InsertEmployee(employee);

                title.com_employees = null;
                result += TitleService.InsertTitle(title);

                if(contacts!= null && contacts.Count > 0)
                    result += ContactService.InsertContacts(contacts);

                Session.NewEmployeeID =+ 1;

                return 1;
            }
            return 0;
        }
    }
}
